#!/bin/sh
 
# $Id: dtemacs,v 2.4 1999/07/25 00:25:50 martin Exp $

# Try to contact Emacs with gnuclient, starting Emacs if no answer.
# Pass all options to gnuclient.  This version is standalone and doesn't
# need any fancy elisp.

GNUCLIENT=gnuclient
GNUCLIENTPINGOPTIONS="-batch -eval t"
EMACS=emacs
TIMEOUT=20

[[ -z $LC_NUMERIC ]] && export LC_NUMERIC=C

# If Emacs doesn't seem to be running then make it so.
if ${GNUCLIENT} ${GNUCLIENTPINGOPTIONS} >/dev/null 2>&1 ; then
    :
else
    ${EMACS} -iconic -eval "(setq gnuserv-kill-quietly t)" -f gnuserv-start 2>&1 >/dev/null &
fi
 
# Try for TIMEOUT seconds to talk to the Emacs process.
 
count=0
until ${GNUCLIENT} ${GNUCLIENTPINGOPTIONS} >/dev/null 2>&1 ; do
    if [ ${count} -gt ${TIMEOUT} ] ; then
        echo "dtemacs: error starting Emacs" 1>&2
        exit 1
    fi
    sleep 1
    count=`expr ${count} + 1`
done

exec ${GNUCLIENT} ${1+"$@"}
